package com.gamenew.bluetoothdemo.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.clj.fastble.data.ScanResult;
import com.gamenew.bluetoothdemo.R;
import com.gamenew.bluetoothdemo.adapter.BluetoothListAdapter;
import com.gamenew.bluetoothdemo.adapter.DeviceListAdapter;
import com.gamenew.bluetoothdemo.bean.BluetoothBean;

import java.util.List;

/**
 * Created by Administrator on 2017/9/18.
 */

public class RankingPopupWindow extends PopupWindow {

    private Listener mListener;
    public DeviceListAdapter mAdapter;

    public RankingPopupWindow(Activity activity, List<ScanResult> list, Listener listener, View view) {

        mListener = listener;

        WindowManager wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();

        //this.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
        this.setWidth(width/2);
        this.setHeight(height/2);
        View contentView = LayoutInflater.from(activity).inflate(R.layout.popup_window_ranking_classify, null);
        this.setContentView(contentView);
        ListView listView = contentView.findViewById(R.id.lv_ranking_classify);

        if (mAdapter != null) {
            mAdapter = null;
        }
        mAdapter = new DeviceListAdapter(activity,list);
        listView.setAdapter(mAdapter);

        this.setFocusable(true);
        this.setAnimationStyle(R.style.AnimList);
        ColorDrawable dw = new ColorDrawable(activity.getResources().getColor(R.color.transparent));
        this.setBackgroundDrawable(dw);
        this.showAsDropDown(view);

        //点击Item
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                dismiss();
                mListener.onItemClickListener(position);
            }
        });
        //点击返回键
        listView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                dismiss();
                return true;
            }
        });

        //消失监听
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                mListener.onPopupWindowDismissListener();
            }
        });

        //点击外围
        this.getContentView().setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                setFocusable(false);
                dismiss();
                return true;
            }
        });
    }



    public interface Listener {
        void onPopupWindowDismissListener();  //弹框消失监听

        void onItemClickListener(int position);  //条目点击监听
    }
}
